/* Copyright (c) 2010-2020 Vadim Surov.
 */
package com.srv.shared;

import java.io.Serializable;
import java.util.Date;

import com.srv.client.Globals;

public class Control implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2464829777355141704L;

	private static int KEY = 0;

	public static enum Type {
		Property("Property"), 
		Text("Text"), 
		Radio("Radio"), 
		Checkbox("Checkbox"), 
		Image("Image"), 
		Line("Line"), 
		Score("Score");

		private final String value;

		Type(String value) {
			this.value = value;
		}

		public String toString() {
			return value;
		}		
	};

	public static Type stringToTypeOld(String type) {
		if (type.equals("Text")) 
			return Type.Text;
		else if (type.equals("Image")) 
			return Type.Image;
		else if (type.equals("RadioOn")) 
			return Type.Radio;
		else if (type.equals("RadioOff")) 
			return Type.Radio;
		else if (type.equals("CheckboxOn")) 
			return Type.Checkbox;
		else if (type.equals("CheckboxOff")) 
			return Type.Checkbox;
		else if (type.equals("Line")) 
			return Type.Line;
		else if (type.equals("Score")) 
			return Type.Score;
		return Type.Property;
	}

	public static Type stringToType(String type) {
		if (type.equals("Property")) 
			return Type.Property; 
		else if (type.equals("Text")) 
			return Type.Text;
		else if (type.equals("Radio")) 
			return Type.Radio;
		else if (type.equals("Checkbox")) 
			return Type.Checkbox;
		else if (type.equals("Image")) 
			return Type.Image;
		else if (type.equals("Line")) 
			return Type.Line;
		else if (type.equals("Score")) 
			return Type.Score;
		return Type.Property;
	}

	private String key;
	private Type type;
	private String value;
	private String text;


	public Control(Type type, String value, String text) {
		this.setKey(""+(KEY++));
		this.setType(type);
		this.setValue(value);
		this.setText(text);
	}

	public Control(String type, String value, String text) {
		this.setKey(""+(KEY++));
		this.setType(type);
		this.setValue(value);
		this.setText(text);
	}

	public Control getControl() {
		return this;
	}	

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}	

	public String getTypeString() {
		return type.toString();
	}

	public void setType(String type) {
		this.type = stringToType(type); 
	}	

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public Control copy() {
		return new Control(getType(), getValue(), getText());
	}

	private String getHtml(String name, boolean enabled, String text) {
		StringBuilder sb = new StringBuilder();		
		switch (type) {
		case Property: 
			sb.append(Globals.html_Control(value, text));
			break;
		case Text: 
			sb.append(Globals.html_Control("", text));
			break;
		case Radio: 
			sb.append(Globals.html_Control_Input(name, "radio", text, 
					(value.equals("checked")?Globals.Checked:Globals.Unchecked), enabled));
			break;
		case Checkbox: 
			sb.append(Globals.html_Control_Input(name, "checkbox", text, 
					(value.equals("checked")?Globals.Checked:Globals.Unchecked), enabled));
			break;
		case Image: 
			sb.append(Globals.html_Control_Image(text));
			break;
		case Line: 
			sb.append(Globals.html_Control_Line);
			break;
		case Score: 
			sb.append(Globals.html_Control("Score", text));
			break;
		default:
			sb.append(Globals.html_Control(value, text));
		}
		return sb.toString();
	}	

	public String getHtml(String name) {
		return getHtml(name, Globals.Enabled, text);
	}	
	
	public String getHtmlEdit(String name) {
		return getHtml(name, Globals.Disabled, Globals.html_TextArea(text));
	}

	public String getToolIcon() {
		return "<div style=\"width:100%; border-style:dotted; border-color:lightgrey; " +
				"border-width:1px;\">" + getHtml("control_"+new Date().getTime()) + "</div>";
	}

	public String getDragIcon() {
		return "<table style=\"border-style:dotted; border-color:lightgrey; " +
				"border-width:1px;\"><tr><td>" + getHtml("control_"+new Date().getTime()) + 
				"</td></tr></table>";
	}
}